package org.mypofolio.model;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Vector;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.mypofolio.model.update.UpdateManager;

public class DataModel {
	private List<DataItem> stocks;
	private SimpleDateFormat sd=new SimpleDateFormat("dd/MM/yyyy");
	
	private Double totD;
	private Double totA;


	

	
	public void load() throws ConfigurationException, ParseException{
		stocks=new ArrayList<DataItem>();
		totD=totA=new Double(0);
		String dir=System.getProperty("user.dir")+File.separator+"stockdata";
		File f= new File(dir);
		File[] all=f.listFiles();
		for (int i = 0; i < all.length; i++) {
			File curr=all[i];
			if (curr.getAbsolutePath().endsWith(".xml")){
				stocks.addAll(loadItems(curr.getAbsolutePath()));
			}
		}
		Collections.sort(stocks,new Comparator<DataItem>() {
			@Override
			public int compare(DataItem o1, DataItem o2) {
				// TODO Auto-generated method stub
				return o1.date.compareTo(o2.date);
			}
		});

	}
	
	private Collection<DataItem> loadItems(String absolutePath) throws ConfigurationException, ParseException {
		System.out.println("loading "+absolutePath);
		XMLConfiguration config = new XMLConfiguration(absolutePath);
		Collection c =new ArrayList<DataItem>();
		int curr=0;
		String stockDes=config.getString("stockdata[@label]");
		while (config.getString("stockdata.op("+curr+")[@desc]")!=null){
			DataItem d1 =new DataItem();
			d1.stockDes=stockDes;
			d1.description=config.getString("stockdata.op("+curr+")[@desc]");
			if (config.getString("stockdata.op("+curr+")[@type]").equals("real")){
				//reale
				d1.amount=config.getDouble("stockdata.op("+curr+")[@amount]");	
				d1.date =sd.parse(config.getString("stockdata.op("+curr+")[@date]"));
				d1.type = DataItem.TYPES.REAL;
			}else{
				//tipo speciale projection:
				d1.date = new GregorianCalendar().getTime();
				d1.type = DataItem.TYPES.PROJ;
			}
			d1.qty=config.getInt("stockdata.op("+curr+")[@qty]");	
			d1.signD=config.getString("stockdata.op("+curr+")[@sign]").equals("D");	
			c.add(d1);
			if (d1.signD){
				totD=totD+d1.totImp();
			}else{
				totA=totA+d1.totImp();
			}
			curr++;			
		}
		
		
		return c;
		
	}

	public List<DataItem> getStocks(){
		return stocks;
	}
	
	public Object[][] toArray(UpdateManager updateManager){		
		Object[][] ret=new Object[stocks.size()+2][7];
		for (int i = 0; i < stocks.size(); i++) {
			DataItem ds	=stocks.get(i);
			ret[i][0]=ds.date;		
			ret[i][1]=ds.stockDes;		
			if (ds.type == DataItem.TYPES.PROJ){	
				updateManager.addToQueue(i, 2, ds.stockDes);
			}else{
				ret[i][2]=ds.description;		
			}

			ret[i][3]=ds.amount;
			ret[i][4]=ds.qty;	
			if (ds.signD){
				ret[i][5]=ds.totImp();	
			}else{
				ret[i][6]=ds.totImp();	
			}
		}
		ret[stocks.size()][5]=totD;
		ret[stocks.size()][6]=totA;	
		if (totD>totA){
			ret[stocks.size()+1][5]=totD-totA;		
		}else{
			ret[stocks.size()+1][6]=totA-totD;			
		}

		
		return ret;
	}


}
